<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class ConfigController extends Controller
{
    /**
     * 站点配置页面
     */
    public function site(){
        $config = $this->getConfig('site');
        return view("admin.config.site",compact('config'));
    }

    /**
     * 基本信息配置界面
     */
    public function info(){
        $config = $this->getConfig('info');
        return view("admin.config.info",compact('config'));
    }

    /**
     * 百度推送配置界面
     */
    public function baidu(){
        $config = $this->getConfig('baidu');
        return view("admin.config.baidu",compact('config'));
    }

    /**
     * 数据入库
     */
    public function store(Request $request){
        $this->checkData($request->name,$request);
        $data = $request->only("name","title");
        $config = json_encode($request->except("name","title","_token"));
        $data['config'] = $config;
        $data['created_at'] = date('Y/m/d H:i:s',time());
        $data['updated_at'] = date('Y/m/d H:i:s',time());
        $config = DB::table('config')->where('name','=',$request->name)->first();
        if($config){
            $result = DB::table('config')->where('name','=',$request->name)->update($data);
        }else{
            $result = DB::table('config')->insert($data);
        }
        if($request === true || $result > 0){
            session()->flash("data",['class'=>'success','msg'=>"更新成功"]);
        }else{
            session()->flash("data",['class'=>'danger','msg'=>"更新失败"]);
        }
        return redirect(route('admin.config.' . $request->name));
    }

    /**
     * 根据配置标识获取配置信息
     */
    private function getConfig($name = 'site'){
        $config_res = DB::table("config")->where(['name'=>$name])->first();
        $config = [];
        if($config_res){
            $config = json_decode($config_res->config,true);
        }
        return $config;
    }
    /**
     * 请求校验
     */
    public function checkData($name = 'site',$request){
        switch ($name){
            case 'info' :
                $request->validate([
                    'company' => 'required',
                    'address' => 'required',
                    'phone'   => 'required',
                ],[
                    'company.required' => '公司名称不能为空',
                    'address.required' => '公司地址不能为空',
                    'phone.required'   => '客服热线不能为空'
                ]);
                break;
            case 'baidu' :
                $request->validate([
                    'key' => 'required'
                ],[
                    'key.required' => '百度秘钥不能为空'
                ]);
                break;
            default :
                $request->validate([
                    'name'      => 'required',
                    'title'     => 'required',
                    'sitetitle' => 'required',
                    'domain'    => 'required|url',
                ],[
                    'name.required'      => '配置标识不能为空',
                    'title.required'     => '配置名称不能为空',
                    'sitetitle.required' => '网站名称不能为空',
                    'domain.required'    => '网站域名不能为空',
                    'domain.url'    => '网址输入不正确',
                ]);
                break;
        }

    }
}
